package com.lenovo.smarttraffic.zm_Set;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lenovo.smarttraffic.InitApp;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager_UserInfo;
import com.lenovo.smarttraffic.database.dao.UserInfoBean;
import com.lenovo.smarttraffic.database.dao.UserInfoBeanDao;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

public class User_SignInActivity extends BaseActivity {

    private static final String TAG = "User_SignInActivity";
    private WebView webView;
    private ProgressBar progress;
    private Intent intent;
    private UserInfoBeanDao userInfoBeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intent = getIntent();
        initView();
        initData();
    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, intent.getStringExtra("title") == null ? "用户签到" : intent.getStringExtra("title"));
        webView = (WebView) findViewById(R.id.webView);
        progress = (ProgressBar) findViewById(R.id.progress);
    }

    private void initData() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new ViewClient());
        webView.setWebChromeClient(new ChromeClient());

        webView.addJavascriptInterface(new JstoAndroid(this), "java");
        Log.e(TAG, "initData: " + intent.getStringExtra("url") + "==1");
        if (intent.getStringExtra("url") != null) {
            webView.loadUrl(intent.getStringExtra("url"));
            return;
        }
        webView.loadUrl("file:///android_asset/index.html");
    }

    class JstoAndroid extends Object {
        private Context context;

        public JstoAndroid(Context context) {
            this.context = context;
        }

        //跳转积分签到
        //webView 重新定向
        //要在此活动内重新启动webView
        //启动点击事件
        //与其js文件 function函数中的方法名 相同才可以重写
        @JavascriptInterface
        public void btnOnClick(String msg) {
            Intent intent = new Intent(context, User_SignInActivity.class);
            intent.putExtra("url", "file:///android_asset/Sign.html");
            intent.putExtra("title", "积分签到");
            Log.e(TAG, "btnOnClick: " + intent.getStringExtra("url") + "==2");
            context.startActivity(intent);
        }

        @JavascriptInterface
        public void imgOnClick(String msg) {
            if (InitApp.signIn) {
                InitApp.signIn = false;
                Toast.makeText(context, "签到成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "已签到", Toast.LENGTH_SHORT).show();
            }
        }



    }

    class ChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progress.setVisibility(View.VISIBLE);
            progress.setProgress(60);
            if (newProgress == 100) {
                progress.setVisibility(View.GONE);
            }
        }
    }

    class ViewClient extends WebViewClient {
        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            InitApp.UserName = "user2";
            DaoManager_UserInfo.getmInstance_UserInfo().init(User_SignInActivity.this);
            userInfoBeanDao = DaoManager_UserInfo.getmInstance_UserInfo().getDaoSession_UserInfo().getUserInfoBeanDao();
            UserInfoBean unique = userInfoBeanDao.queryBuilder().where(UserInfoBeanDao.Properties.Username.eq(InitApp.UserName)).list().get(0);
            String name = unique.getPname();
            String tel = unique.getPtel();
            Log.e(TAG, "onPageFinished: " + name + tel);
            view.loadUrl("javascript:javaCallJsName('" + name + "','" + tel + "')");
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_user__sign_in;
    }

}
